package com.lagou.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.lagou.dao.AccountDao;
import com.lagou.dao.impl.JdbcAccountDaoImpl;
import com.lagou.service.AccountService;
import com.lagou.service.impl.AccountServiceImpl;
import com.lagou.util.ConnectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration // 表明当前类是一个配置类
@ComponentScan({"com.lagou"}) //开启注解扫描
@PropertySource({"classpath:jdbc.properties"})
@EnableTransactionManagement //开启spring注解事务的⽀持
/*@Import()*/
public class SpringConfig {

    @Value("${jdbc.driver}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

//    @Bean // 未指定 bean 的名称，默认采用的是 "方法名" + "首字母小写"的配置方式
//    public DataSourceTransactionManager transactionManager(DataSource dataSource){
//        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
//        transactionManager.setDataSource(dataSource);
//        return transactionManager;
//    }

//    @Bean("accountService")
//    public AccountService createAccountService(AccountDao accountDao){
//        AccountService accountService = new AccountServiceImpl(accountDao);
//        return accountService;
//    }

//    @Bean("accountDao")
//    public AccountDao createAccountDao(ConnectionUtils connectionUtils){
//        AccountDao accountDao = new JdbcAccountDaoImpl(connectionUtils);
//        return  accountDao;
//    }

    @Bean("dataSource")
    public DataSource createDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driverClassName);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return  druidDataSource;
    }

    @Bean // 未指定 bean 的名称，默认采用的是 "方法名" + "首字母小写"的配置方式
    public JdbcTemplate jdbcTemplate(DataSource dataSource){
        JdbcTemplate jdbcTemplate=new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }



}
